package controllers;

import java.util.ArrayList;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import beans.RepositoryBeanRemote;
import dto.RepositoryDTO;

@ManagedBean
public class RepositoryDTOController {
	@EJB
	RepositoryBeanRemote rb;

	@ManagedProperty(value = "#{repositoryDTO}")
	private RepositoryDTO rDTO;
	
	private String renderCreateRepository;
	private String renderChangeSupervisor;

	private String selectedUser; //contains the selected user name from userList in createRep.xhtml
	private ArrayList<String> allUsers = new ArrayList<String>(); //list of all user names
	private ArrayList<String> matchingUsers = new ArrayList<String>(); //list of matching user names 

	private String selectedRep; //contains the selected repository name form repList in changeSup.xhtml
	private ArrayList<String> allRepositories = new ArrayList<String>(); // list of all repository names
	private ArrayList<String> matchingRepositories = new ArrayList<String>(); // list of matching repository names


	/**
	 * Description: method invoked at keyup gets all matching users
	 * 
	 * @author Zsolt
	 * @param event
	 */
	public void supNameListener(AjaxBehaviorEvent event) {
		allUsers = ((ArrayList<String>) rb.getAllUserNames().getAllUsers());
		matchingUsers.clear();
		for (String s : allUsers) {
			if (s.startsWith(rDTO.getUserID())) {
				matchingUsers.add(new String(s));
			}
		}
		if (matchingUsers.size()!=0){
			selectedUser = matchingUsers.get(0);
		} else {
			selectedUser = "";
		}
	}

	/**
	 * Description: set value of selectedUser
	 * @author Zsolt
	 * @param event
	 */
	public void userListListener(AjaxBehaviorEvent event) {
		rDTO.setUserID(selectedUser);
		matchingUsers.clear();
		matchingUsers.add(selectedUser);
	}

	/**
	 * Description: method invoked at keyup gets all matching repositories
	 * 
	 * @author Zsolt
	 * @param event
	 */
	public void repNameListener(AjaxBehaviorEvent event) {
		allRepositories = ((ArrayList<String>) rb.getAllRepositoryNames()
				.getAllRepositories());
		matchingRepositories.clear();
		for (String s : allRepositories) {
			if (s.startsWith(rDTO.getRepName())) {
				matchingRepositories.add(new String(s));
			}
		}
		if (matchingRepositories.size()!=0){
			selectedRep = matchingRepositories.get(0);
		} else {
			selectedRep = "";
		}
	}

	/**
	 * Description: set value of selectedRep
	 * @author Zsolt
	 * @param event
	 */
	public void repListListener(AjaxBehaviorEvent event) {
		rDTO.setRepName(selectedRep);
		matchingRepositories.clear();
		matchingRepositories.add(selectedRep);
	}
	
	/**
	 * Description: set value of selectedRep
	 * @author Zsolt
	 * @param event
	 */
	public void viewListListener(AjaxBehaviorEvent event) {
		allRepositories = ((ArrayList<String>) rb.getAllRepositoryNames()
				.getAllRepositories());
		rDTO.setRepName(selectedRep);
	}

	/**
	 * Description: method that adds a new repository
	 * @author Zsolt
	 * @return String ok
	 */
	public String addRepository() {
		// internationalization
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ////////
		try {
			rb.addRepositoryDTO(rDTO);

			renderCreateRepository = messageBundle
					.getString("repositoryCreatedSuccessfully");
		} catch (Exception e) {
			renderCreateRepository = messageBundle
					.getString("repositoryCouldNotBeCreated");

		}
		return "ok";
	}

	public String getRenderCreateRepository() {
		return renderCreateRepository;
	}

	public void setRenderCreateRepository(String renderCreateRepository) {
		this.renderCreateRepository = renderCreateRepository;
	}

	public String popUpOk() {
		renderCreateRepository = null;
		renderChangeSupervisor = null;
		return "createRep.xhtml?faces-redirect=true";
	}

	public String popUpOkSup() {
		renderCreateRepository = null;
		renderChangeSupervisor = null;
		return "createSup.xhtml?faces-redirect=true";
	}

	/**
	 * Description: method that deletes a new repository
	 * @author Zsolt
	 * @return String ok
	 */
	public String deleteRepository() {
		rb.deleteRepository(rDTO.getId());
		return "ok";
	}

	/**
	 * Description: method that changes supervisor of a repository
	 * 
	 * @author Zsolt
	 * @return String ok
	 */
	public String changeSupervisor() {
		// internationalization
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ////////
		try {
			renderChangeSupervisor = messageBundle
					.getString("supervisorChangedSuccessfully");
			rb.changeSupervisor(rDTO);

		} catch (Exception e) {
			renderChangeSupervisor = messageBundle
					.getString("supervisorCouldNotBeChanged");

		}

		return "ok";
	}
	/**
	 * Description method that set RepID to repository that we want to see + redirect 
	 * @author Zsolt
	 * @return String to redirect link
	 */
	public String viewRepository(){
		rDTO.setRepID(rb.getRepID(rDTO));
		return "AdminMainPage.xhtml?faces-redirect=true";
	}

	public RepositoryDTO getRepository() {
		return rDTO;
	}

	public void setRepository(RepositoryDTO rDTO) {
		this.rDTO = rDTO;
	}

	public String getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(String selectedUser) {
		this.selectedUser = selectedUser;
	}

	public ArrayList<String> getMatchingUsers() {
		return matchingUsers;
	}

	public void setMatchingUsers(ArrayList<String> matchingUsers) {
		this.matchingUsers = matchingUsers;
	}

	public ArrayList<String> getAllUsers() {
		return allUsers;
	}

	public void setAllUsers(ArrayList<String> allUsers) {
		this.allUsers = allUsers;
	}

	public String getSelectedRep() {
		return selectedRep;
	}

	public void setSelectedRep(String selectedRep) {
		this.selectedRep = selectedRep;
	}

	public ArrayList<String> getAllRepositories() {
		return allRepositories;
	}

	public void setAllRepositories(ArrayList<String> allRepositories) {
		this.allRepositories = allRepositories;
	}

	public ArrayList<String> getMatchingRepositories() {
		return matchingRepositories;
	}

	public void setMatchingRepositories(ArrayList<String> matchingRepositories) {
		this.matchingRepositories = matchingRepositories;
	}

	public RepositoryDTO getrDTO() {
		return rDTO;
	}

	public void setrDTO(RepositoryDTO rDTO) {
		this.rDTO = rDTO;
	}


	public String getRenderChangeSupervisor() {
		return renderChangeSupervisor;
	}

	public void setRenderChangeSupervisor(String renderChangeSupervisor) {
		this.renderChangeSupervisor = renderChangeSupervisor;
	}

}
